package com.css.bjdt_report.util.tags;

import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;

import org.nutz.lang.Lang;

/**
 * select标签
 */
public class SelectTag extends TagSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -9049765299272180960L;

	/**
	 * 生成option集合，目前只支持map
	 */
	private Object items;

	private String value;
	private String headerValue;
	private String headerKey;
	private String name;
	private String id;

	private String onchange;
	private String cssClass;
	private String cssStyle;

	@Override
	public void setPageContext(PageContext pageContext) {
		super.setPageContext(pageContext);
		value = "";
		headerValue = "";
		headerKey = "";
		name = "";
		id = "";

		onchange = "";
		cssClass = "";
		cssStyle = "";
	}

	@Override
	public void release() {
		items = null;
		value = null;
		headerValue = null;
		headerKey = null;
		name = null;
		id = null;
		onchange = null;
		cssClass = null;
		cssStyle = null;
	}

	@Override
	@SuppressWarnings("unchecked")
	public int doEndTag() throws JspException {
		JspWriter jspOut = pageContext.getOut();
		try {
			jspOut.append("<select id=\"" + getId() + "\" name=\"" + name + "\" " + getOnchange());
			if (cssClass != null) {
				jspOut.append("class=\"" + cssClass + "\" ");
			}
			if (cssStyle != null) {
				jspOut.append("style=\"" + cssStyle + "\" ");
			}
			jspOut.append(" >");
			jspOut.append("<option value=\"" + headerValue + "\">" + headerKey + "</option>");
			if (items != null) {
				//Collection itemsList = (Collection) items;
				Map<String, Object> codeMap = (Map<String, Object>) items;
				for (Entry<String, Object> entry : codeMap.entrySet()) {
					if (value != null && value.equals(entry.getKey())) {
						jspOut.append("<option value=\"" + entry.getKey() + "\" selected = \"selected\">"
								+ entry.getValue().toString() + "</option>");
					} else {
						jspOut.append("<option value=\"" + entry.getKey() + "\">" + entry.getValue().toString()
								+ "</option>");
					}

				}
			}
			jspOut.append("</select>");
		} catch (Exception e) {
			throw Lang.wrapThrow(e);
		} finally {
			value = null;
			headerValue = null;
			name = null;
			id = null;
			onchange = null;
		}

		return EVAL_PAGE;
	}

	@Override
	public String getId() {
		if (id == null) {
			id = name;
		}
		return id;
	}

	public void setName(String name) {
		this.name = name;
	}

	@Override
	public void setId(String id) {
		this.id = id;
	}

	public void setItems(Object items) {
		this.items = items;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getHeaderValue() {
		return headerValue;
	}

	public void setHeaderValue(String headerValue) {
		this.headerValue = headerValue;
	}

	public String getHeaderKey() {
		return headerKey;
	}

	public void setHeaderKey(String headerKey) {
		this.headerKey = headerKey;
	}

	public Object getItems() {
		return items;
	}

	public String getName() {
		return name;
	}

	public void setOnchange(String onchange) {
		this.onchange = onchange;
	}

	public String getOnchange() {
		if (onchange == null) {
			return "";
		}
		return "onchange=\"" + onchange + "\"";

	}

	public String getCssClass() {
		return cssClass;
	}

	public void setCssClass(String cssClass) {
		this.cssClass = cssClass;
	}

	public String getCssStyle() {
		return cssStyle;
	}

	public void setCssStyle(String cssStyle) {
		this.cssStyle = cssStyle;
	}

}
